package assignment2;

import java.awt.*;
import java.awt.Color;
import java.lang.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import util.EasyInput;
import csimage.UberImage;


public class ImageBlendPane extends JPanel {
	
	private JButton save;
	private JPanel image1, image2, image3;
	private UberImage img1, img2, img3, img;
	private UberImage copy1, copy2;
	                    
	public ImageBlendPane() {
		JSlider slider = new JSlider(JSlider.VERTICAL, 0,
				100, 0);
		slider.addChangeListener(new SliderListener());
		slider.setMajorTickSpacing(50);
		slider.setMinorTickSpacing(5);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);

		img1 = UberImage.fromFile(EasyInput.chooseFile());
		copy1 = UberImage.blankImage(img1.getWidth(), img1.getHeight(), Color.white);
		img2 = UberImage.fromFile(EasyInput.chooseFile());
		copy2 = UberImage.blankImage(img2.getWidth(), img2.getHeight(), Color.white);
		img3 = img2;
		save = new JButton("Save");
		save.setBackground(Color.LIGHT_GRAY);
		save.addActionListener(new SaveListener());
		
		image3 = new Image3Panel();
		image3.setPreferredSize(new Dimension(img2.getWidth(), img2.getHeight()));
		

		this.add(image3);
		this.add(slider);
		this.add(save);
	}

	private class Image1Panel extends JPanel {
		public void paint(Graphics g) {
			g.drawImage(img1, 0, 0, null);
		}
	}
	
	private class Image2Panel extends JPanel {
		public void paint(Graphics g) {
			g.drawImage(img2, 0, 0, null);
		}
	}
	
	private class Image3Panel extends JPanel {
		public void paint(Graphics g) {
			g.drawImage(img3, 0, 0, null);
		}
	}
	
	private class SliderListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			JSlider source = (JSlider) e.getSource();
			if (!source.getValueIsAdjusting()) {
				double thresh = source.getValue();
				thresh = (thresh / 100);
				double thresh2 = (1 - thresh);
				for (int i =0; i < img1.getWidth(); ++i) {
					for (int j = 0; j < img1.getHeight(); ++j) {
						int c1 = img1.getRGB(i, j);
						int c = img2.getRGB(i, j);
						double red = ((((c1 & 0x00ff0000) >> 16) * thresh) + (((c & 0x00ff0000) >> 16) * thresh2));
						double green = ((((c1 & 0x0000ff00) >> 8) * thresh) + (((c & 0x0000ff00) >> 8) * thresh2));
						double blue = ((((c1 & 0x000000ff)) * thresh) + (((c & 0x000000ff)) * thresh2));
						if (red > 255)
							red = 255;
						if (red < 0)
							red = 0;
						if (green > 255)
							green = 255;
						if (green < 0)
							green = 0;
						if (blue > 255)
							blue = 255;
						if (blue < 0)
							blue = 0;
						copy1.setColor(i, j, new Color((int)red, (int)green, (int)blue));
					}
				}
				img3 = copy1;
				repaint();
			}
		}
	}
	
	private class SaveListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			img3.saveAsJPEG(EasyInput.chooseFile());
		}
	}
}